package com.utils.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @Desc：list排序
 * @Title：TestListSort
 * @author: zhaoyangyang
 * @version: 1.0 
 * @date: 2017年4月21日 下午2:53:30
 */
public class TestListSort implements Comparable<TestListSort>{

	private String name;
    private Integer order;
    private String age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getOrder() {
        return order;
    }
    public void setOrder(Integer order) {
        this.order = order;
    }
    public int compareTo(TestListSort arg0) {
        return this.getOrder().compareTo(arg0.getOrder());
    }
    public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public static void main1(String[] args) {
		//实现接口排序
    	TestListSort user1 = new TestListSort();
        user1.setName("a");
        user1.setOrder(1);
        TestListSort user2 = new TestListSort();
        user2.setName("b");
        user2.setOrder(2);
        List<TestListSort> list = new ArrayList<TestListSort>();
        //此处add user2再add user1
        list.add(user2);
        list.add(user1);
        Collections.sort(list);
        for(TestListSort u : list){
            System.out.println(u.getName());
        }
    }
    
    public static void main(String[] args) {
    	//该方法不实现implements Comparable<TestListSort>接口
    	TestListSort user1 = new TestListSort();
        user1.setName("a");
        user1.setOrder(1);
        TestListSort user2 = new TestListSort();
        user2.setName("b");
        user2.setOrder(2);
        List<TestListSort> list = new ArrayList<TestListSort>();
        list.add(user2);
        list.add(user1);
        
        Collections.sort(list,new Comparator<TestListSort>(){
            public int compare(TestListSort arg0, TestListSort arg1) {
                return arg0.getOrder().compareTo(arg1.getOrder());
            }
        });
        for(TestListSort u : list){
            System.out.println(u.getName());
        }
	}
    
    public static void main2(String[] args) {
    	TestListSort user1 = new TestListSort();
        user1.setName("a");
        user1.setOrder(1);
        TestListSort user2 = new TestListSort();
        user2.setName("b");
        user2.setOrder(2);
        List<TestListSort> list = new ArrayList<TestListSort>();
        list.add(user2);
        list.add(user1);
        //多字段排序
		Collections.sort(list, new Comparator<TestListSort>() {
			public int compare(TestListSort arg0, TestListSort arg1) {
				// 第一次比较专业
				int i = arg0.getOrder().compareTo(arg1.getOrder());

				// 如果专业相同则进行第二次比较
				if (i == 0) {
					// 第二次比较
					int j = arg0.getName().compareTo(arg1.getName());
					// 如果学制相同则返回按年龄排序
					if (j == 0) {
						return arg0.getAge().compareTo(arg1.getAge());
					}
					return j;
				}
				return i;
			}
		});
	}
    
    
}
